package com.running.base.run.util;

import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class MapUtil {

	/**
	 * 字符串-封装成map集合
	 * @param result
	 * @return
	 */
	public static Map<String, String> getResultMap(String result) {
		Map<String, String> map = new HashMap<String, String>();
		String[] re1 = result.split("&");
		for (int i=0;i<re1.length;i++) {
			String[] mes = re1[i].split("=");
			map.put(mes[0], mes[1]);
		}
		return map;
	}
	
	/**
	 * get请求-封装成map集合
	 * @param request
	 * @return
	 */
	public static Map<String, Object> formatRequest(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Map<String, String[]> map = request.getParameterMap();
			String _key;
			Object _value;
			for (Enumeration<String> names = request.getParameterNames(); names.hasMoreElements(); result.put(_key, _value)) {
				StringBuilder sb = new StringBuilder();
				Object key = names.nextElement();
				Object values[] = (Object[]) map.get(key);
				_key = (String) key;
				_value = values.length <= 1 ? values[0] : ((Object) (values));
				sb.append(_key);
				sb.append(" = |");
				Object aobj[];
				int j = (aobj = values).length;
				for (int i = 0; i < j; i++) {
					Object value = aobj[i];
					sb.append((String) value);
					sb.append("|");
				}

			}

		} catch (Exception e) {
			System.out.println("异常信息：" + e.getMessage());
		}
		
		return result;
	}
	
	public static void splitFieldToOrderedMap(Map<String, Object> map, Object obj) throws Throwable {
        Field[] fields = FieldUtil.getAllFields(obj.getClass());
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.isSynthetic()) {
                continue;
            }
            Object fieldObj = field.get(obj);
            String fieldName = field.getName();
            if (fieldObj == null || "".equals(fieldObj) || fieldName == "sign" || fieldName == "sign_type") {
                continue;
            }
            map.put(field.getName(), field.get(obj));
        }
    }
	
}
